<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <el-menu
            default-active="1"
            class="el-menu-demo"
            mode="horizontal"
            background-color="#1E90FF"
            text-color="#fff"
            active-text-color="#87CEFA"
            :ellipsis="false"
        >
          <el-menu-item index="/admin/index" @click="router.push('/admin/index')" style="width: 200px">
            <img src="@/assets/Convergence.png" style="width: 150px; height: 50px;">
          </el-menu-item>
          <el-sub-menu index="2">
            <template #title>{{ admin.username }}</template>
            <el-menu-item index="center" @click="updateDialogShow=true;" style="color:#FFF;">个人中心</el-menu-item>
            <el-menu-item index="chaPwd" @click="chgPwdDialogShow=true;" style="color:#FFF;" >修改密码</el-menu-item>
            <el-menu-item index="logout" @click="logout" style="color:#FFF;">退出</el-menu-item>

          </el-sub-menu>
        </el-menu>
      </el-header>
      <el-container>
        <el-aside width="200px" class="left-menu-aside">
          <el-menu
              active-text-color="#E0E0E0"
              background-color="#F5F5F5"
              class="el-menu-vertical-demo"
              text-color="#fff"
              :router="true"
          >
            <el-menu-item index="/admin/index" style="color: #666666">
              <el-icon>
                <HomeFilled/>
              </el-icon>
              后台首页
            </el-menu-item>

            <el-sub-menu index="1" >
              <template #title >
                <el-icon style="color: #666666">
                  <Avatar/>
                </el-icon>
                <span style="color: #666666">管理员管理</span>
              </template>
              <el-menu-item index="/admin/admin" style="color: #666666">管理员列表</el-menu-item>
            </el-sub-menu>

            <el-sub-menu index="2">
              <template #title>
                <el-icon style="color: #666666">
                  <Menu/>
                </el-icon>
                <span style="color: #666666">商品分类管理</span>
              </template>
              <el-menu-item index="/admin/category" style="color: #666666">分类列表</el-menu-item>
            </el-sub-menu>

            <el-sub-menu index="3">
              <template #title>
                <el-icon style="color: #666666">
                  <Grid/>
                </el-icon>
                <span style="color: #666666">商品管理</span>
              </template>
              <el-menu-item index="/admin/goods" style="color: #666666">商品列表</el-menu-item>
            </el-sub-menu>


            <el-sub-menu index="4">
              <template #title>
                <el-icon style="color: #666666">
                  <UserFilled/>
                </el-icon>
                <span style="color: #666666">用户管理</span>
              </template>
              <el-menu-item index="/admin/user" style="color: #666666">用户列表</el-menu-item>
            </el-sub-menu>


            <el-sub-menu index="5">
              <template #title>
                <el-icon style="color: #666666">
                  <ShoppingCart/>
                </el-icon>
                <span style="color: #666666">订单管理</span>
              </template>
              <el-menu-item index="/admin/order" style="color: #666666">订单列表</el-menu-item>
            </el-sub-menu>

          </el-menu>
        </el-aside>
        <el-main>
          <RouterView/>
        </el-main>
      </el-container>
    </el-container>
  </div>

  <!-- 修改密码对话框开始 -->
  <el-dialog v-model="chgPwdDialogShow" title="修改密码" width="500">
    <el-form>
      <el-form-item label="原密码" label-width="20%">
        <el-input v-model="chgPwdObj.oldPwd" placeholder="请输入原密码" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="新密码" label-width="20%">
        <el-input v-model="chgPwdObj.newPwd" placeholder="请输入新密码" autocomplete="off"/>
      </el-form-item>

      <el-form-item label="新密码" label-width="20%">
        <el-input v-model="chgPwdObj.newPwd1" placeholder="请输入新密码" autocomplete="off"/>
      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="chgPwdDialogShow = false">取消</el-button>
        <el-button type="primary" @click="chgPwd">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
  <!--修改密码的对话框结束-->

  <!-- 修改信息对话框开始 -->
  <el-dialog v-model="updateDialogShow" title="修改信息" width="500">
    <el-form>
      <el-form-item label="用户名" label-width="20%">
        <el-input v-model="admin.username" disabled autocomplete="off"/>
      </el-form-item>
      <el-form-item label="电话" label-width="20%">
        <el-input v-model="admin.phone" placeholder="请输入电话" autocomplete="off"/>
      </el-form-item>

      <el-form-item label="电子邮箱" label-width="20%">
        <el-input v-model="admin.email" placeholder="请输入电子邮箱" autocomplete="off"/>
      </el-form-item>

      <el-form-item label="真实姓名" label-width="20%">
        <el-input v-model="admin.realname" placeholder="请输入真实姓名" autocomplete="off"/>
      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="updateDialogShow = false">取消</el-button>
        <el-button type="primary" @click="chgInfo">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
  <!--修改信息的对话框结束-->


</template>

<script setup>
import router from "@/router/index.js";
import {Avatar, HomeFilled, UserFilled} from "@element-plus/icons-vue";
import {useTokenStore} from "@/stores/token.js";
import adminApi from "@/api/adminApi.js";
import {ref} from "vue";
import {ElMessage} from "element-plus";

const tokenStore = useTokenStore();

//管理员信息
const admin = ref({
  username: null,
  phone: null,
  email: null,
  realname: null
});

function logout() {
  //将token重置
  tokenStore.$reset();
  //跳转到管理员登录页面
  router.push("/admin/login");
}

//是否显示修改密码对话框
const chgPwdDialogShow = ref(false);

//是否显示修改管理员信息的对话框
const updateDialogShow = ref(false);

//保存修改密码的对象
const chgPwdObj = ref({
  oldPwd: null,
  newPwd: null,
  newPwd1: null
});

//修改密码
function chgPwd() {
  adminApi.chaPwd(chgPwdObj.value)
      .then(resp => {
        if (resp.code == 10000) {
          ElMessage.success(resp.msg);
          //重新登录
          logout();
          //清空信息
          chgPwdObj.value = {
            oldPwd: null,
            newPwd: null,
            newPwd1: null
          };
        } else {
          ElMessage.error(resp.msg);
        }
      });
}

//修改个人信息
function chgInfo() {
  adminApi.chgInfo(admin.value)
      .then(resp => {
        if (resp.code == 10000) {
          ElMessage.success(resp.msg);
          updateDialogShow.value = false;
        } else {
          ElMessage.error(resp.msg);
        }
        getInfo();
      });
}

//获取已登录用户信息
function getInfo() {
  adminApi.getInfo()
      .then(resp => {
        admin.value = resp.data;
      });
}

getInfo();
</script>

<style scoped>
.el-menu--horizontal > .el-menu-item:nth-child(1) {
  margin-right: auto;
}

.el-header {
  padding: 0px;
}

.el-container, el-menu {
  height: 100%;
}

.left-menu-aside {
  height: 100vh; /* 设置高度为视口高度，确保菜单铺满一列 */
}

.left-menu-aside .el-menu {
  height: 100%; /* 确保菜单内部的元素也能铺满高度 */
}

</style>